﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Volo.Abp;

namespace Puppy.ActorDb.Query
{

    public class ActorQueryable<TEntity> : IOrderedActorQueryable<TEntity>
    {
        private readonly IActorQueryProvider _queryProvider;
        private readonly Expression _expression;

        public ActorQueryable(IActorQueryProvider queryProvider)
        {
            _queryProvider = Check.NotNull(queryProvider, nameof(queryProvider));
            _expression = Expression.Constant(this, typeof(IActorQueryable<TEntity>));
        }

        public ActorQueryable(IActorQueryProvider queryProvider, Expression expression)
        {
            _queryProvider = Check.NotNull(queryProvider, nameof(queryProvider));
            _expression = Check.NotNull(expression, nameof(expression));
        }

        public Type ElementType
        {
            get { return typeof(TEntity); }
        }

        public Expression Expression
        {
            get { return _expression; }
        }

        public IQueryProvider Provider
        {
            get { return _queryProvider; }
        }

        public IEnumerator<TEntity> GetEnumerator()
        {
            var results = (IEnumerable<TEntity>)_queryProvider.Execute(_expression);
            return results.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
}
